package com.xoa.service.emergency;

import com.xoa.dao.emergency.CrashDispatchMapper;
import com.xoa.dao.emergency.CrashDispatchParentMapper;
import com.xoa.model.emergency.CrashDispatchParentWithBLOBs;
import com.xoa.model.emergency.CrashDispatchWithBLOBs;
import com.xoa.util.LimsDataTree;
import com.xoa.util.LimsJson;
import com.xoa.util.page.PageParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CrashDispatchParentService {

    @Resource
    CrashDispatchParentMapper crashDispatchParentMapper;

    @Autowired
    CrashDispatchService crashDispatchService;

    @Resource
    private CrashDispatchMapper crashDispatchMapper;

    public LimsJson insert(HttpServletRequest request,CrashDispatchParentWithBLOBs crashDispatchParentWithBLOBs){

        LimsJson toJson = new LimsJson(1, "err");
        try {
            int result = crashDispatchParentMapper.insert(crashDispatchParentWithBLOBs);
            if (result > 0) {
                CrashDispatchWithBLOBs crashDispatchWithBLOBs = new CrashDispatchWithBLOBs();
                crashDispatchWithBLOBs.setCrasId(crashDispatchParentWithBLOBs.getCrasId());
                crashDispatchWithBLOBs.setPareId(crashDispatchParentWithBLOBs.getCrasPareId());
                crashDispatchWithBLOBs.setEventName(crashDispatchParentWithBLOBs.getEventName());
                crashDispatchWithBLOBs.setHappenTime(crashDispatchParentWithBLOBs.getHappenTime());
                crashDispatchWithBLOBs.setHappenPlace(crashDispatchParentWithBLOBs.getHappenPlace());
                crashDispatchWithBLOBs.setEventDescribe(crashDispatchParentWithBLOBs.getEventDescribe());
                crashDispatchWithBLOBs.setRescueUser(crashDispatchParentWithBLOBs.getRescueUser());
                crashDispatchWithBLOBs.setReactionUser(crashDispatchParentWithBLOBs.getReactionUser());
                crashDispatchWithBLOBs.setRescueUserName(crashDispatchParentWithBLOBs.getRescueUserName());
                crashDispatchWithBLOBs.setReactionUserName(crashDispatchParentWithBLOBs.getReactionUserName());
                int res = crashDispatchMapper.insertSelective(crashDispatchWithBLOBs);
                /**
                 * 增加救援事件后发送事务提醒和短信提醒
                 */
                if (res > 0) {
                    String strs = crashDispatchWithBLOBs.getRescueUser() + crashDispatchWithBLOBs.getReactionUser();
                    crashDispatchService.AttendRemind(request,strs,"Uid",crashDispatchWithBLOBs.getCrasId());
                    toJson.setCode(0);
                    toJson.setMsg("新增成功");
                }else{
                    crashDispatchParentMapper.deleteByPrimaryKey(crashDispatchParentWithBLOBs.getCrasPareId());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return toJson;
    }


    public LimsJson showCrashDispById(Integer crasPareId) {
        LimsJson toJson = new LimsJson(1, "err");
        try {
            Map map = new HashMap();
            List<CrashDispatchWithBLOBs> bsList = crashDispatchMapper.selectByCrasPareId(crasPareId);
            toJson.setCode(0);
            toJson.setMsg("查询成功");
            toJson.setData(bsList);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return toJson;
    }


    public LimsJson queryAll(Integer page, Integer limit) {
        LimsJson toJson = new LimsJson(1, "err");
        try {
            Map map = new HashMap();
            PageParams pageParams = new PageParams();
            pageParams.setUseFlag(true);
            pageParams.setPageSize(limit);
            pageParams.setPage(page);
            map.put("page", pageParams);
            List<CrashDispatchParentWithBLOBs> list = crashDispatchParentMapper.selectAll(map);
            for(CrashDispatchParentWithBLOBs crashDispatchParentWithBLOBs : list){
                List<CrashDispatchWithBLOBs> bsList = crashDispatchMapper.selectByCrasPareId(crashDispatchParentWithBLOBs.getCrasPareId());
                if(bsList.size() > 1){
                    crashDispatchParentWithBLOBs.setCrashDispatchList(bsList);
                }else if(bsList.size() == 1){
                    CrashDispatchWithBLOBs crashDispatchWithBLOBs = bsList.get(0);
                    crashDispatchParentWithBLOBs.setCrasId(crashDispatchWithBLOBs.getCrasId());
                    crashDispatchParentWithBLOBs.setCrasPareId(crashDispatchWithBLOBs.getPareId());
                    crashDispatchParentWithBLOBs.setEventName(crashDispatchWithBLOBs.getEventName());
                    crashDispatchParentWithBLOBs.setHappenTime(crashDispatchWithBLOBs.getHappenTime());
                    crashDispatchParentWithBLOBs.setHappenPlace(crashDispatchWithBLOBs.getHappenPlace());
                    crashDispatchParentWithBLOBs.setEventDescribe(crashDispatchWithBLOBs.getEventDescribe());
                    crashDispatchParentWithBLOBs.setRescueUser(crashDispatchWithBLOBs.getRescueUser());
                    crashDispatchParentWithBLOBs.setRescueUserName(crashDispatchWithBLOBs.getRescueUserName());
                    crashDispatchParentWithBLOBs.setReactionUser(crashDispatchWithBLOBs.getReactionUser());
                    crashDispatchParentWithBLOBs.setReactionUserName(crashDispatchWithBLOBs.getReactionUserName());
                }
            }
            if (list != null && list.size() > 0) {
                toJson.setCode(0);
                toJson.setMsg("查询成功");
                toJson.setCount(pageParams.getTotal());
                toJson.setData(list);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return toJson;
    }

    public LimsJson queryAll() {
        LimsJson toJson = new LimsJson(1, "err");
        try {
            List<CrashDispatchParentWithBLOBs> list = crashDispatchParentMapper.selectAll(null);
            if (list != null && list.size() > 0) {
                toJson.setCode(0);
                toJson.setMsg("查询成功");
                toJson.setCount(list.size());
                toJson.setData(list);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return toJson;
    }

    public List<LimsDataTree> queryAllTree() {
        List<LimsDataTree> limsDataTrees = new ArrayList<>();
        try {
            List<CrashDispatchParentWithBLOBs> list = crashDispatchParentMapper.selectAll(null);
            if (list != null && list.size() > 0) {
              for (CrashDispatchParentWithBLOBs crashDispatchParentWithBLOBs:list){
                  LimsDataTree limsDataTree = new LimsDataTree();
                  List<CrashDispatchWithBLOBs> bsList = crashDispatchMapper.selectByCrasPareId(crashDispatchParentWithBLOBs.getCrasPareId());
                  if(bsList != null && bsList.size()>1){
                      List<LimsDataTree> limsDataTreesChildren = new ArrayList<>();
                      for(CrashDispatchWithBLOBs crashDispatchWithBLOBs:bsList){
                          LimsDataTree limsDataTreeChildren = new LimsDataTree();
                          limsDataTreeChildren.setName(crashDispatchWithBLOBs.getEventName());
                          limsDataTreeChildren.setId(crashDispatchWithBLOBs.getCrasId());
                          limsDataTreeChildren.setIsLeaf(false);
                          limsDataTreesChildren.add(limsDataTreeChildren);
                      }
                      limsDataTree.setName(crashDispatchParentWithBLOBs.getEventName());
                      limsDataTree.setId(crashDispatchParentWithBLOBs.getCrasPareId());
                      limsDataTree.setIsLeaf(true);
                      limsDataTree.setChildren(limsDataTreesChildren);
                  }else if(bsList.size() ==1){
                      limsDataTree.setName(crashDispatchParentWithBLOBs.getEventName());
                      limsDataTree.setId(bsList.get(0).getCrasId());
                      limsDataTree.setIsLeaf(false);
                  }
                  limsDataTrees.add(limsDataTree);
              }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return limsDataTrees;
    }

}
